package com.ly.hotel.view.common.msg;


import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class MyToast {
	
	private int mGravity = Gravity.BOTTOM|Gravity.RIGHT;
	private int mX, mY,width;
	protected Context context;
	private final LayoutParams mParams = new LayoutParams();
	WindowManager mWindowManager;
	float mHorizontalMargin;
    float mVerticalMargin;
    View mView;
    final Handler mHandler = new Handler();  
	
	public MyToast(Context context){
		this.context = context;
		
		mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		
		final LayoutParams params = mParams;
          params.height = LayoutParams.WRAP_CONTENT;
          params.width = LayoutParams.WRAP_CONTENT;
//          params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//                  | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
//                  | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
          params.type = LayoutParams.TYPE_SYSTEM_ALERT | LayoutParams.TYPE_SYSTEM_OVERLAY;
  		  params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;

          params.format = PixelFormat.TRANSLUCENT;
//          params.windowAnimations = com.android.internal.R.style.Animation_Toast;
//          params.type = WindowManager.LayoutParams.TYPE_TOAST;
          params.setTitle("Toast");
          
	}
	


	public void setView(View mView) {
		this.mView = mView;
		mWindowManager.addView(mView, mParams);
	}
	
	public void setView(int layout) {
		 LayoutInflater inflate = (LayoutInflater)
         context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflate.inflate(layout, null);
		this.mView = v;
		mWindowManager.addView(mView, mParams);
	}

	public void setGravity(int gravity,int with, int xOffset, int yOffset) {
	        mGravity = gravity;
	        mX = xOffset;
	        mY = yOffset;
	        width = with;
	    }
	  public void setMargin(float horizontalMargin, float verticalMargin) {
	        mHorizontalMargin = horizontalMargin;
	        mVerticalMargin = verticalMargin;
	    }
	  
	  public void setFocusable(boolean focusable){
		  if(focusable){
			  mParams.flags = 0;
		  }else{
			  mParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
		  }
	  }

	 public void handleShow() {
		 if(mView!=null){
             final int gravity = mGravity;
             mParams.gravity = gravity;
             mParams.width = width;
             if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
                 mParams.horizontalWeight = 1.0f;
             }
             if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
                 mParams.verticalWeight = 1.0f;
             }
             mParams.x = mX;
             mParams.y = mY;
             mParams.verticalMargin = mVerticalMargin;
             mParams.horizontalMargin = mHorizontalMargin;
             if (mView.getParent() != null) {
            	 ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).removeView(mView);
             }
             ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).addView(mView, mParams);
             
		 }
     }
	
	 public void handleHide() {
         if (mView != null) {
             // note: checking parent() just to make sure the view has
             // been added...  i have seen cases where we get here when
             // the view isn't yet added, so let's try not to crash.
             if (mView.getParent() != null) {
            	 ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).removeView(mView);
             }

             mView = null;
         }
     }
	 
	 final Runnable mShow = new Runnable() {
         public void run() {
             handleShow();
         }
     };

     final Runnable mHide = new Runnable() {
         public void run() {
             handleHide();
         }
     };
     
     public void show() {
         mHandler.post(mShow);
     }

     /**
      * schedule handleHide into the right thread
      */
     public void hide() {
         mHandler.post(mHide);
     }
     
     public void remove() {
    	 try {
    	   if (mView != null) {
    		   ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).removeView(mView);
               mView = null;
           }
    	 } catch (Exception e) {
    		 e.printStackTrace();
    	 }
     }

}
